package com.btpn.custody.web.m002.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import com.btpn.custody.bean.ParameterBeans;
import com.btpn.custody.common.Constant;
import com.btpn.custody.dao.IAddress;
import com.btpn.custody.dao.IArea;
import com.btpn.custody.dao.IMasterData;
import com.btpn.custody.dao.IUser;
import com.btpn.custody.entity.Address;
import com.btpn.custody.entity.MasterData;
import com.btpn.custody.entity.User;
import com.btpn.custody.service.IBranchService;
import com.btpn.custody.web.m002.model.AllAddress;
import com.btpn.custody.web.m002.model.BranchModel;
import com.btpn.custody.web.m002.model.Param;
import com.btpn.infra.utils.MyDateUtils;
import com.opensymphony.xwork2.ActionSupport;

@ParentPackage(value = "custody")
@Results({
	@Result(name="input", type="redirectAction", params = {"actionName", "checklist"} )
})
public class ChecklistAction extends ActionSupport implements ServletRequestAware {
	
	/*=============================================== C O N S T A N T =========================================*/
	private static final long serialVersionUID = 2021280758565695106L;
	
	/*=============================================== M E M B E R =============================================*/		
	@Autowired
	private IAddress addressDaoHibernate;
	
	@Autowired
	private IArea areaDaoHibernate;
	
	@Autowired
	private IBranchService branchService;
	
	@Autowired
	private IMasterData masterDataDaoHibernate;
	
	@Autowired
	private IUser userDaoHibernate;

	@Autowired
	private ParameterBeans parameterBeans;
	
	private HttpServletRequest servletRequest;
	
//	private String areaName;
	private BranchModel model;
	private List<Param> branchList;					// list of KCP/KC	
	
	private MasterData master;
	private AllAddress srcAddress;
	
	private String BIRTH_DATE;
	private String ID_EXP_DATE;
	private String TDP_EXP_DATE;
	private String OPEN_DATE;

	// SEARCH
	private String keyword, fromSearch, toSearch, accCode, cstCode, branchCode;	
	
	// parameter
	private Map<String, String> idTypeMap;			// P0102
	private Map<String, String> religionMap;		// P0108
	private Map<String, String> eduMap;				// P0109
	private Map<String, String> currIncomeMap;		// P0110
	private Map<String, String> srcIncomeMap;		// P0111
	private Map<String, String> purposeMap;			// P0112
	private Map<String, String> maritalMap;			// P0118
	private Map<String, String> hubAhliWarisMap;	// P0124
	private Map<String, String> lapKeuMap;			// P0137
	private Map<String, String> hubBankMap;			// P0140
	private Map<String, String> golNasabahMap;		// P0141
	private Map<String, String> hrCstMap;			// P0142
	private Map<String, String> hrBussMap;			// P0143
	private Map<String, String> hrCountryMap;		// P0144
	private Map<String, String> hrProductMap;		// P0145
	private Map<String, String> addIncomeMap;		// P0146
	private Map<String, String> countryMap;			// P0001	
	private Map<String, String> cstTypeMap;			// P0002
	private Map<String, String> accTypeMap;			// P0003
	
//	private List<Param> lID_TYPE;				// P0102
//	private List<Param> lRELIGION;				// P0108
//	private List<Param> lEDUCATION;				// P0109
//	private List<Param> lCURRENT_INCOME;		// P0110
//	private List<Param> lSOURCE_INCOME;			// P0111
//	private List<Param> lPURPOSE;				// P0112
//	private List<Param> lMARITAL_STATUS;		// P0118
//	private List<Param> lHUB_AHLI_WARIS;		// P0124
//	private List<Param> lLAP_KEUANGAN;			// P0137
//	private List<Param> lHUB_DENGAN_BANK;		// P0140
//	private List<Param> lLBU_GOL_NASABAH;		// P0141
//	private List<Param> lHR_CUSTOMER;			// P0142
//	private List<Param> lHR_BUSINESS;			// P0143
//	private List<Param> lHR_COUNTRIES;			// P0144
//	private List<Param> lHR_PRODUCT;			// P0145
//	private List<Param> lADDITIONAL_INCOME;		// P0146
//	private List<Param> lCOUNTRY;				// P0001
//	private List<Param> lCST_TYPE;				// P0002
//	private List<Param> lACC_TYPE;				// P0003
	
	/*=============================================== B E H A V I O R =========================================*/	
	@Action(value = "/checklist", results = { @Result(name="success", location="t_checklist") } )
	public String checklist() throws Exception {
		
		User user = userDaoHibernate.get(SecurityContextHolder.getContext().getAuthentication().getName());
		branchList = branchService.findBranchMap(user.getArea().getId(), Constant.STATUS__MD_COMPLETED);
		accTypeMap = parameterBeans.getmACC_TYPE(); 
		cstTypeMap = parameterBeans.getmCST_TYPE();
		keyword = null; fromSearch = null; toSearch = null; accCode = null; cstCode = null; branchCode = null;
		
		return SUCCESS;
	}

	@Action(value = "/searchChecklist", results = { 
			@Result(name="success", location="t_checklist")
	})
	public String searchChecklist() throws Exception {
		Date from = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, fromSearch);
		Date to = MyDateUtils.strToDate(Constant.FORMAT_PRESENTATION, toSearch);

		User user = userDaoHibernate.get(SecurityContextHolder.getContext().getAuthentication().getName());
		model = branchService.search(user.getArea().getId(), Constant.STATUS__MD_COMPLETED, keyword.trim(), branchCode, accCode, cstCode, from, to);

		branchList = branchService.findBranchMap(user.getArea().getId(), Constant.STATUS__MD_COMPLETED);
		accTypeMap = parameterBeans.getmACC_TYPE(); 
		cstTypeMap = parameterBeans.getmCST_TYPE();
		
		return SUCCESS;
	}
		
	/*
	 * next : move to service
	 */
	@Action(value = "/checklistDetail", results = { 
			@Result(name=Constant.TYPE_INDIVIDU, 		location="t_individu"),
			@Result(name=Constant.TYPE_NON_INDIVIDU, 	location="t_non_individu"),
			@Result(name="none", 						location="t_checklist")
	})
	public String showDataDetail() throws Exception {
		
		// init master parameter
		idTypeMap = parameterBeans.getmID_TYPE();
		religionMap = parameterBeans.getmRELIGION();
		eduMap = parameterBeans.getmEDUCATION();
		currIncomeMap = parameterBeans.getmCURRENT_INCOME();
		srcIncomeMap = parameterBeans.getmSOURCE_INCOME();
		purposeMap = parameterBeans.getmPURPOSE();
		maritalMap = parameterBeans.getmMARITAL_STATUS();
		hubAhliWarisMap = parameterBeans.getmHUB_AHLI_WARIS();
		lapKeuMap = parameterBeans.getmLAP_KEUANGAN();
		hubBankMap = parameterBeans.getmHUB_DENGAN_BANK();
		golNasabahMap = parameterBeans.getmLBU_GOL_NASABAH();
		hrCstMap = parameterBeans.getmHR_CUSTOMER();
		hrBussMap = parameterBeans.getmHR_BUSINESS();
		hrCountryMap = parameterBeans.getmHR_COUNTRIES();
		hrProductMap = parameterBeans.getmHR_PRODUCT();
		addIncomeMap = parameterBeans.getmADDITIONAL_INCOME();		
		countryMap = parameterBeans.getmCOUNTRY();
//		cstTypeMap = parameterBeans.getmCST_TYPE();
//		accTypeMap = parameterBeans.getmACC_TYPE();
		
		// get MasterData by dataId (harusnya CIF)
		String id = getServletRequest().getParameter("dataId");
		master = masterDataDaoHibernate.get(id);
				
		// add uncomplete fields/docs
		String[] errors = StringUtils.split(master.getSTATUS_MD_MSG(), "*");
		for (String s : errors) {
			if (s.length()!=1) { addActionError(s); }
		}			
		
		// init all address
		srcAddress = new AllAddress();
		for (Address address : addressDaoHibernate.findByMD(master.getSid())) {
			if (address.getADDRESS_TYPE().equals(Address.PRIME_ADDRESS)) {  
				srcAddress.setAddress31(address.getADDRESS1());
				srcAddress.setAddress32(address.getADDRESS2());
				srcAddress.setAddress33(address.getADDRESS3());
				srcAddress.setAddress34(address.getADDRESS4());
				srcAddress.setAddress35(address.getADDRESS5());
				srcAddress.setPostCode3(address.getPOST_CODE());
			}
			if (address.getADDRESS_TYPE().equals(Address.HOME_ADDRESS)) {  
				srcAddress.setAddress11(address.getADDRESS1());
				srcAddress.setAddress12(address.getADDRESS2());
				srcAddress.setAddress13(address.getADDRESS3());
				srcAddress.setAddress14(address.getADDRESS4());
				srcAddress.setAddress15(address.getADDRESS5());
				srcAddress.setPostCode1(address.getPOST_CODE());
			}
			if (address.getADDRESS_TYPE().equals(Address.OFFICE_ADDRESS)) {  
				srcAddress.setAddress21(address.getADDRESS1());
				srcAddress.setAddress22(address.getADDRESS2());
				srcAddress.setAddress23(address.getADDRESS3());
				srcAddress.setAddress24(address.getADDRESS4());
				srcAddress.setAddress25(address.getADDRESS5());
				srcAddress.setPostCode2(address.getPOST_CODE());
			}
			if (address.getADDRESS_TYPE().equals(Address.EMAILING_ADDRESS)) {  
				srcAddress.setAddress81(address.getADDRESS1());
				srcAddress.setAddress82(address.getADDRESS2());
				srcAddress.setAddress83(address.getADDRESS3());
				srcAddress.setAddress84(address.getADDRESS4());
				srcAddress.setAddress85(address.getADDRESS5());
				srcAddress.setPostCode8(address.getPOST_CODE());
			}
		}
		
		// date parameters
		OPEN_DATE = MyDateUtils.dateToStr(Constant.FORMAT_PRESENTATION, master.getCIF().getOPEN_DATE());
		if (StringUtils.contains(Constant.CST_INDIVIDU, master.getCIF().getCUSTOMER_TYPE())) {
			BIRTH_DATE = MyDateUtils.dateToStr(Constant.FORMAT_PRESENTATION, master.getCIF().getBIRTH_DATE());
			ID_EXP_DATE = MyDateUtils.dateToStr(Constant.FORMAT_PRESENTATION, master.getCIF().getID_EXPIRED());
			
			return Constant.TYPE_INDIVIDU;
		} else if (StringUtils.contains(Constant.CST_NON_INDIVIDU, master.getCIF().getCUSTOMER_TYPE())) {
			TDP_EXP_DATE = MyDateUtils.dateToStr(Constant.FORMAT_PRESENTATION, master.getCIF().getTGL_AKHIR_TDP());
			
			return Constant.TYPE_NON_INDIVIDU;
		} else {
			return NONE;
		}
	}
	
	/*=============================================== G E T T E R   &   S E T T E R ===========================*/
	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.servletRequest = request;
	}
	
	public Map<String, String> getAccTypeMap() {
		return accTypeMap;
	}

	public void setAccTypeMap(Map<String, String> accTypeMap) {
		this.accTypeMap = accTypeMap;
	}

	public Map<String, String> getCstTypeMap() {
		return cstTypeMap;
	}

	public void setCstTypeMap(Map<String, String> cstTypeMap) {
		this.cstTypeMap = cstTypeMap;
	}

	public String getAccCode() {
		return accCode;
	}

	public void setAccCode(String accCode) {
		this.accCode = accCode;
	}

	public String getCstCode() {
		return cstCode;
	}

	public void setCstCode(String cstCode) {
		this.cstCode = cstCode;
	}

	public List<Param> getBranchList() {
		return branchList;
	}

	public void setBranchList(List<Param> branchList) {
		this.branchList = branchList;
	}

	public MasterData getMaster() {
		return master;
	}

	public void setMaster(MasterData master) {
		this.master = master;
	}

	public AllAddress getSrcAddress() {
		return srcAddress;
	}

	public void setSrcAddress(AllAddress srcAddress) {
		this.srcAddress = srcAddress;
	}

//	public String getAreaName() {
//		return areaName;
//	}
//
//	public void setAreaName(String areaName) {
//		this.areaName = areaName;
//	}
	
//	public List<BranchModel> getModels() {
//		return models;
//	}
//
//	public void setModels(List<BranchModel> models) {
//		this.models = models;
//	}

	public BranchModel getModel() {
		return model;
	}

	public void setModel(BranchModel model) {
		this.model = model;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public String getFromSearch() {
		return fromSearch;
	}

	public void setFromSearch(String fromSearch) {
		this.fromSearch = fromSearch;
	}

	public String getToSearch() {
		return toSearch;
	}

	public void setToSearch(String toSearch) {
		this.toSearch = toSearch;
	}

	public String getBranchCode() {
		return branchCode;
	}

	public void setBranchCode(String branchCode) {
		this.branchCode = branchCode;
	}

	public String getBIRTH_DATE() {
		return BIRTH_DATE;
	}

	public void setBIRTH_DATE(String bIRTH_DATE) {
		BIRTH_DATE = bIRTH_DATE;
	}

	public String getID_EXP_DATE() {
		return ID_EXP_DATE;
	}

	public void setID_EXP_DATE(String iD_EXP_DATE) {
		ID_EXP_DATE = iD_EXP_DATE;
	}

	public String getTDP_EXP_DATE() {
		return TDP_EXP_DATE;
	}

	public void setTDP_EXP_DATE(String tDP_EXP_DATE) {
		TDP_EXP_DATE = tDP_EXP_DATE;
	}

	public String getOPEN_DATE() {
		return OPEN_DATE;
	}

	public void setOPEN_DATE(String oPEN_DATE) {
		OPEN_DATE = oPEN_DATE;
	}

	public ParameterBeans getParameterBeans() {
		return parameterBeans;
	}

	public void setParameterBeans(ParameterBeans parameterBeans) {
		this.parameterBeans = parameterBeans;
	}

	public Map<String, String> getIdTypeMap() {
		return idTypeMap;
	}

	public void setIdTypeMap(Map<String, String> idTypeMap) {
		this.idTypeMap = idTypeMap;
	}

	public IArea getAreaDaoHibernate() {
		return areaDaoHibernate;
	}

	public void setAreaDaoHibernate(IArea areaDaoHibernate) {
		this.areaDaoHibernate = areaDaoHibernate;
	}

	public Map<String, String> getReligionMap() {
		return religionMap;
	}

	public void setReligionMap(Map<String, String> religionMap) {
		this.religionMap = religionMap;
	}

	public Map<String, String> getEduMap() {
		return eduMap;
	}

	public void setEduMap(Map<String, String> eduMap) {
		this.eduMap = eduMap;
	}

	public Map<String, String> getCurrIncomeMap() {
		return currIncomeMap;
	}

	public void setCurrIncomeMap(Map<String, String> currIncomeMap) {
		this.currIncomeMap = currIncomeMap;
	}

	public Map<String, String> getSrcIncomeMap() {
		return srcIncomeMap;
	}

	public void setSrcIncomeMap(Map<String, String> srcIncomeMap) {
		this.srcIncomeMap = srcIncomeMap;
	}

	public Map<String, String> getPurposeMap() {
		return purposeMap;
	}

	public void setPurposeMap(Map<String, String> purposeMap) {
		this.purposeMap = purposeMap;
	}

	public Map<String, String> getMaritalMap() {
		return maritalMap;
	}

	public void setMaritalMap(Map<String, String> maritalMap) {
		this.maritalMap = maritalMap;
	}

	public Map<String, String> getHubAhliWarisMap() {
		return hubAhliWarisMap;
	}

	public void setHubAhliWarisMap(Map<String, String> hubAhliWarisMap) {
		this.hubAhliWarisMap = hubAhliWarisMap;
	}

	public Map<String, String> getLapKeuMap() {
		return lapKeuMap;
	}

	public void setLapKeuMap(Map<String, String> lapKeuMap) {
		this.lapKeuMap = lapKeuMap;
	}

	public Map<String, String> getHubBankMap() {
		return hubBankMap;
	}

	public void setHubBankMap(Map<String, String> hubBankMap) {
		this.hubBankMap = hubBankMap;
	}

	public Map<String, String> getGolNasabahMap() {
		return golNasabahMap;
	}

	public void setGolNasabahMap(Map<String, String> golNasabahMap) {
		this.golNasabahMap = golNasabahMap;
	}

	public Map<String, String> getHrCstMap() {
		return hrCstMap;
	}

	public void setHrCstMap(Map<String, String> hrCstMap) {
		this.hrCstMap = hrCstMap;
	}

	public Map<String, String> getHrBussMap() {
		return hrBussMap;
	}

	public void setHrBussMap(Map<String, String> hrBussMap) {
		this.hrBussMap = hrBussMap;
	}

	public Map<String, String> getHrCountryMap() {
		return hrCountryMap;
	}

	public void setHrCountryMap(Map<String, String> hrCountryMap) {
		this.hrCountryMap = hrCountryMap;
	}

	public Map<String, String> getHrProductMap() {
		return hrProductMap;
	}

	public void setHrProductMap(Map<String, String> hrProductMap) {
		this.hrProductMap = hrProductMap;
	}

	public Map<String, String> getAddIncomeMap() {
		return addIncomeMap;
	}

	public void setAddIncomeMap(Map<String, String> addIncomeMap) {
		this.addIncomeMap = addIncomeMap;
	}

	public Map<String, String> getCountryMap() {
		return countryMap;
	}

	public void setCountryMap(Map<String, String> countryMap) {
		this.countryMap = countryMap;
	}
}